/**
 * 
 */
package info.niwota.webapp;


import info.niwota.commons.Debug;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

/**
 * @author qiangli
 *
 */
public class InstallHotwebActivity extends Activity {

	private static final String TAG = "InstallHotwebActivity";

	private static final String HOTWEB_PKG = "info.niwota.web";
	
	private static final String INSTALL_PKG_URL = "market://search?q=pname:";

	private static final String EXTRA_PKG = "extra.pkg";

	private static final String EXTRA_TITLE = "extra.title";

	private static final String EXTRA_MESSAGE = "extra.message";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//
		if (Debug.DEBUG) {
			Log.d(TAG, "onCreate " + savedInstanceState);
		}
		Intent i = getIntent();
		final String pkg = i.getStringExtra(EXTRA_PKG);
		
		//hotweb
		if (pkg == null) {
			showDownload();
			return;
		}
		
		//other apps
		final String title = i.getStringExtra(EXTRA_TITLE);
		final String msg = i.getStringExtra(EXTRA_MESSAGE);
		
		showDownload(pkg, title, msg);
	}

	private void showDownload() {
		final CharSequence title = "Hotweb by Li, Qiang";
		final CharSequence msg = "Hotweb (free) is required to support this feature. Download Hotweb from Android Market?";
		showDownload(HOTWEB_PKG, title, msg);
	}

	private void showDownload(final String pkg, CharSequence title, CharSequence msg) {
		final CharSequence yes = "Yes";
		final CharSequence no = "No";
		//
		final Context ctx = this;
		
		AlertDialog d = new AlertDialog.Builder(ctx).create();
		d.setIcon(android.R.drawable.ic_dialog_info);
		d.setTitle(title);
		d.setMessage(msg);
		d.setButton(yes, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				try {
					Uri uri = Uri.parse(INSTALL_PKG_URL + pkg);
					Intent i = new Intent(Intent.ACTION_VIEW, uri);
					startActivity(i);
				} catch (Exception e) {
					Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_SHORT);
				}
				finish();
				return;
			}
		});
		d.setButton2(no, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(ctx, "Cannceled", Toast.LENGTH_SHORT);
				finish();
				return;
			}
		});
		d.show();
	}
	
	/**
	 * Ensure Hotweb has been installed
	 * @param ctx
	 * @return
	 */
	public static boolean ensure(Context ctx) {
		boolean installed = isHotwebAvailable(ctx);
		if (Debug.DEBUG) {
			Log.d(TAG, "ensure: installed? " + installed);
		}
		if (!installed) {
			Intent i = new Intent(ctx, InstallHotwebActivity.class);
			ctx.startActivity(i);
		}
		return installed;
	}
	
	/**
	 * Start installing Hotweb
	 * 
	 * @param ctx
	 */
	public static void startActivity(Context ctx) {
		if (Debug.DEBUG) {
			Log.d(TAG, "startActivity");
		}
		Intent i = new Intent(ctx, InstallHotwebActivity.class);
		ctx.startActivity(i);
	}
	
	/**
	 * Prompt user before installing an application
	 * 
	 * @param ctx
	 * @param pkg
	 * @param title
	 * @param message
	 */
	public static void installApp(Context ctx, String pkg, String title, String message) {
		if (Debug.DEBUG) {
			Log.d(TAG, "startActivity: " + pkg);
		}
		Intent i = new Intent(ctx, InstallHotwebActivity.class);
		i.putExtra(EXTRA_PKG, pkg);
		i.putExtra(EXTRA_TITLE, title);
		i.putExtra(EXTRA_MESSAGE, message);
		ctx.startActivity(i);
	}
	
	public static boolean isHotwebAvailable(Context ctx) {
		final String pkg = "info.niwota.web";
		final String clz = "info.niwota.web.ServerActivity";
		return isAppAvailable(ctx, pkg, clz);
	}
	
	public static boolean isAppAvailable(Context ctx, String pkg, String clz) {
		final Intent i = createAppIntent(pkg, clz);
		return isAppAvailable(ctx, i);
	}
	
	public static boolean isAppAvailable(Context ctx, Intent app) {
		final PackageManager pm = ctx.getPackageManager();
		List<ResolveInfo> l = pm.queryIntentActivities(app,
				PackageManager.MATCH_DEFAULT_ONLY);
		return (l.size() > 0);
	}

	public static Intent createAppIntent(String pkg, String clz) {
		final Intent i = new Intent(Intent.ACTION_GET_CONTENT);
		i.setClassName(pkg, clz);
		return i;
	}
}
